/*
 * Socket.h
 *
 *  Created on: 2017年8月1日
 *      Author: heqian
 */

#ifndef QQ_SOCKET_H_
#define QQ_SOCKET_H_
#include <string>
#include <sys/select.h>
#include "Prt.h"

namespace utils {

class Socket {
public:
	enum Type {
		NONE, CILENT, SERVER
	};

	Socket();
	Socket(Socket* socket);
	virtual ~Socket();

	bool Server(int port, int backlog);
	bool Connect(std::string url, int port = 80);
	void Close();
	int Send(char* data, int length);
	int Recv(char* data, int length);
	int Select(float time = 1); //套字节 time 等待时间
	int getPort();
	Prt<Socket> Accept();

	Type getType();
	std::string getError();
private:
	int mSocket;
	std::string mError;
	Type mType;
	fd_set mRfds;

	bool init(int port = 0);
};

} /* namespace qq */

#endif /* QQ_SOCKET_H_ */
